unit WinVer;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This file is part of the PasCalc Calculator

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode Delphi}{$H+}

interface

uses Windows, SysUtils, ShlObj;

type

  TWinVer = (WinUnknown, Win9x, Win2000, WinXP, WinVista, Win7);
  TLang = (lngOther, lngRu, lngEn);

function WinVersion: TWinVer;
function GetUserAppDataFolderPath : String;
function GetUserMyDocumentsFolderPath : String;
function GetUserFavoritesFolderPath : String;

function GetCommonAppDataFolderPath : String;

function GetWindowsFolerPath : String;
function GetTempFolderPath : String;
function WinLang: TLang;

implementation

const
  {$EXTERNALSYM CSIDL_COMMON_APPDATA}
  CSIDL_COMMON_APPDATA = $0023;

function GetSpecialFolderPath(CSIDL : Integer) : String;
var
  Path : PChar;
begin
  Result := '';
  GetMem(Path,MAX_PATH);
  Try
    If Not SHGetSpecialFolderPath(0,Path,CSIDL,False) Then
      Raise Exception.Create('Shell function SHGetSpecialFolderPath fails.');
    Result := Trim(StrPas(Path));
    If Result = '' Then
      Raise Exception.Create('Shell function SHGetSpecialFolderPath return an empty string.');
    Result := IncludeTrailingPathDelimiter(Result);
  Finally
    FreeMem(Path,MAX_PATH);
  End;
end;

function GetTempFolderPath : String;
var
  Path : PChar;
begin
  Result := ExtractFilePath(ParamStr(0));
  GetMem(Path,MAX_PATH);
  Try
    If GetTempPath(MAX_PATH,Path) <> 0 Then
      Begin
        Result := Trim(StrPas(Path));
        Result := IncludeTrailingPathDelimiter(Result);
      End;
  Finally
    FreeMem(Path,MAX_PATH);
  End;
end;

function WinLang: TLang;
var
  ID: integer;
begin
  ID := GetSystemDefaultLangID;
end;

function GetWindowsFolerPath : String;
var
  Path : PChar;
begin
  Result := ExtractFilePath(ParamStr(0));
  GetMem(Path,MAX_PATH);
  Try
    If GetWindowsDirectory(Path, MAX_PATH) <> 0 Then
      Begin
        Result := Trim(StrPas(Path));
        Result := IncludeTrailingPathDelimiter(Result);
      End;
  Finally
    FreeMem(Path,MAX_PATH);
  End;
end;

function GetUserAppDataFolderPath : String;
begin
  Result := GetSpecialFolderPath(CSIDL_APPDATA);
end;

function GetUserMyDocumentsFolderPath : String;
begin
  Result := GetSpecialFolderPath(CSIDL_PERSONAL);
end;

function GetUserFavoritesFolderPath : String;
begin
  Result := GetSpecialFolderPath(CSIDL_FAVORITES);
end;

function GetCommonAppDataFolderPath : String;
begin
  Result := GetSpecialFolderPath(CSIDL_COMMON_APPDATA);
end;

function WinVersion: TWinVer;
var
  osVerInfo: TOSVersionInfo;
begin
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  if GetVersionEx(osVerInfo) then
    with osVerInfo do
    begin
      if dwMajorVersion < 5 then result := Win9x;
      if dwMajorVersion = 5 then
      begin
        if dwMinorVersion = 0 then result := Win2000 else
          if (dwMinorVersion = 1) or (dwMinorVersion = 2) then result := WinXP;
      end;
      if dwMajorVersion = 6 then
        if dwMinorVersion = 0 then result := WinVista else
          if dwMinorVersion = 1 then result := Win7;
    end
  else
    Result := WinUnknown
end;

end.

